package com.dream.controller;

import com.alibaba.fastjson.JSON;
import com.dream.pojo.Clazz;
import com.dream.pojo.Major;
import com.dream.pojo.Student;
import com.dream.service.ClazzService;
import com.dream.service.DepartmentService;
import com.dream.service.MajorService;
import com.dream.service.StudentService;
import com.dream.service.impl.ClazzServiceImpl;
import com.dream.service.impl.DepartmentServiceImpl;
import com.dream.service.impl.MajorServiceImpl;
import com.dream.service.impl.StudentServiceImpl;
import com.dream.utils.PageUtil;
import com.dream.utils.ResultCommon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ClazzController.do")
public class ClazzController extends BaseServlet{
    ClazzService clazzService = new ClazzServiceImpl();
    MajorService majorService = new MajorServiceImpl();
    DepartmentService departmentService=new DepartmentServiceImpl();
    StudentService studentService=new StudentServiceImpl();


    /**
     * 分页查询
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clazzName = request.getParameter("clazzName"); //条件1
        String pageIndex = request.getParameter("pageIndex"); //条件4  当前页码
        if (pageIndex == null || "".equals(pageIndex)) {
            pageIndex = "1";//默认值
        }
        String pageSize = request.getParameter("pageSize"); //条件5   页面大小
        if (pageSize == null || "".equals(pageSize)) {
            pageSize = "5";//默认值
        }
        //条数
        long totalCount = clazzService.totalCount(clazzName);
        //每页数据
        List<Clazz> clazzList = clazzService.list(clazzName, Long.valueOf(pageIndex), Long.valueOf(pageSize));
        for (Clazz clazz : clazzList) {
            Major major = majorService.findById(clazz.getMajorId());
            clazz.setMajorName(major.getMajorName());
        }
        //封装一个分页的工具类
        PageUtil<Clazz> pageUtil = new PageUtil<Clazz>(Long.valueOf(pageIndex), Long.valueOf(pageSize), totalCount, clazzList);
        //存值转发
        request.setAttribute("pageUtil", pageUtil);
        request.setAttribute("clazzName", clazzName); //回显
        request.getRequestDispatcher("viewClazzInfo.jsp").forward(request, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultCommon resultCommon = null;
        String clazzId = request.getParameter("clazzId");
        String majorId = request.getParameter("majorId");
        String clazzName = request.getParameter("clazzName");
        String remark = request.getParameter("remark");
        Clazz clazz = new Clazz(clazzId, majorId, clazzName, remark);
        int count = clazzService.add(clazz);
        if (count > 0) {
            resultCommon = new ResultCommon(200, "新增成功", null);
        } else {
            resultCommon = new ResultCommon(500, "新增异常", null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void goupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String clazzId=request.getParameter("clazzId");
        Clazz clazz = clazzService.findById(clazzId);

        String majorId = clazz.getMajorId();
        Major major = majorService.findById(majorId);
        String deptId = major.getDeptId();

        request.setAttribute("deptId",deptId);
        request.setAttribute("majorId",majorId);

        request.setAttribute("clazz",clazz);
        request.getRequestDispatcher("updateClazz.jsp").forward(request,response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String clazzId = request.getParameter("clazzId");
        String majorId = request.getParameter("majorId");
        String clazzName = request.getParameter("clazzName");
        String remark = request.getParameter("remark");
        Clazz clazz = new Clazz(clazzId, majorId, clazzName, remark);
        int count = clazzService.update(clazz);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"更新成功",null);
        } else {
            resultCommon=new ResultCommon(500,"更新异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }
    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String clazzId = request.getParameter("clazzId");
        //判断班级下是否有学生
        List<Student> students = studentService.findStudentsByClazzId(clazzId);
        if(students.size()>0){
            response.getWriter().write("<script>alert('班级在使用中，勿删除！');location.href='/ClazzController.do?action=list';</script>");
        }else{
            int count = clazzService.deleteById(clazzId);
            if(count>0){
                response.getWriter().write("<script>alert('班级删除成功');location.href='/ClazzController.do?action=list';</script>");
            }else{
                response.getWriter().write("<script>alert('班级删除异常');location.href='/ClazzController.do?action=list';</script>");
            }
        }
    }

    public void getClazzsByMajorId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String majorId = request.getParameter("majorId");
        ResultCommon resultCommon = null;
        //判断班级下是否有学生
        List<Clazz> clazzList = clazzService.findClazzByMajorId(majorId);
        if (clazzList.size() > 0) {
            resultCommon = new ResultCommon(200, "查询成功", clazzList);
        } else {
            resultCommon = new ResultCommon(500, "查询异常", null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }
}
